package com.dankegongyu.lib.recyclerview

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import com.dankegongyu.lib.R
import com.dankegongyu.lib.base.BaseAppActivity
import com.dankegongyu.lib.common.util.DKToast
import com.dankegongyu.lib.common.widget.recyclerview.layoutmanger.DKLinearLayoutManager
import kotlinx.android.synthetic.main.activity_test_recycler.*

/**
 *
 * @author wpq
 * @version 1.0
 */
class TestRecyclerActivity : BaseAppActivity() {

    private val mAdapter: TestAdapter by lazy { TestAdapter(mutableListOf()) }
    private val mHeaderViews = mutableListOf<View>()
    private val mFooterViews = mutableListOf<View>()

    override fun getContentViewLayoutId(): Int {
        return R.layout.activity_test_recycler
    }

    override fun init(savedInstanceState: Bundle?) {
        btn_addHeader.setOnClickListener {
            val headerView = getHeader()
            recyclerview.addHeaderView(headerView)
            mHeaderViews.add(headerView)
        }
        btn_removeHeader.setOnClickListener {
//            DKLog.e("headers: " + mHeaderViews.size)
            if (mHeaderViews.isNotEmpty()) {
                val index = mHeaderViews.size - 1
                recyclerview.removeHeaderView(mHeaderViews[index])
                mHeaderViews.removeAt(index)
            }
        }
        btn_addFooter.setOnClickListener {
            val footerView = getFooter()
            recyclerview.addFooterView(footerView)
            mFooterViews.add(footerView)
        }

        btn_removeFooter.setOnClickListener {
//            DKLog.e("headers: " + mHeaderViews.size)
            if (mFooterViews.isNotEmpty()) {
                val index = mFooterViews.size - 1
                recyclerview.removeFooterView(mFooterViews[index])
                mFooterViews.removeAt(index)
            }
        }

        btn_addItem.setOnClickListener {
//            val list = listOf("新增项1", "新增项2")
//            mAdapter.addData(list)
            mAdapter.addData(1, "新增项")
            recyclerview.notifyItemInserted(1, mAdapter.itemCount)
        }

        btn_addItemRanged.setOnClickListener {
            val list = listOf("新增项1", "新增项2")
            mAdapter.addData(1, list)
            recyclerview.notifyItemRangeInserted(1, 2, mAdapter.itemCount)
        }

        btn_removeItemRanged.setOnClickListener {
            var removeCount = 0
            for (i in 0..1) {
                val removed = mAdapter.removeData(1)
                removed?.let { removeCount++ }
            }
            if (removeCount > 0) {
                recyclerview.notifyItemRangeRemoved(1, removeCount, mAdapter.itemCount)
            }
        }

        recyclerview.layoutManager = DKLinearLayoutManager(this)
//        recyclerview.itemAnimator = null // 尝试fix：Called attach on a child which is not detached: ViewHolder
        recyclerview.adapter = mAdapter
        mAdapter.setOnItemClickListener { viewHolder, position, itemData ->
            mAdapter.removeData(itemData)
            DKToast.show("remove: " + position)
//            mAdapter.notifyItemRemoved(recyclerview.headersCount + position)
            recyclerview.notifyItemRemoved(position, mAdapter.itemCount)
        }
    }

    override fun initPresenter() {
    }

    override fun doBusiness() {
        for (i in 0..4) {
            mAdapter.addData("第" + i + "项")
        }
        mAdapter.notifyDataSetChanged()
    }

    private fun getHeader() = ImageView(this).apply {
        layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 88)
        scaleType = ImageView.ScaleType.FIT_XY
        setImageResource(R.drawable.ic_room_filter_tab_arrow_down_normal)
    }

    private fun getFooter() = ImageView(this).apply {
        layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 88)
        scaleType = ImageView.ScaleType.FIT_XY
        setImageResource(R.drawable.ic_room_filter_tab_arrow_down_selected)
    }
}